package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_Category;

import com.openbravo.bean.ProductCategory;
import com.openbravo.bean.erp.OperationProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class OperationProductCategoryDAO extends ModifiedLogDAO<OperationProductCategory>{
   
    public OperationProductCategoryDAO(I_Session s) {
        super(s);
    }

    
     public TableDefinition getTable() {
         return new TableDefinition(s,
                "OPERATIONPRODUCTCATEGORIES",
                new Field[]{
                   new Field(I_Category.ID,Datas.STRING, Formats.STRING,"Label.Name"),
                   new Field(I_Category.NAME,Datas.STRING, Formats.STRING),
                   new Field(I_Category.PARENTID,Datas.STRING, Formats.STRING,ProductCategory.class),
                   new Field(I_Category.ISHOT,Datas.BOOLEAN, Formats.BOOLEAN,"label.categoryPanel.ishot"),
                   new Field(I_Category.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                } ,
                new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, OperationProductCategory obj) throws BasicException {
           dp.setString(1, obj.getID());
           dp.setString(2, obj.getName());
           dp.setString(3, obj.getM_sParentID());
           dp.setBoolean(4, obj.isHot());
           dp.setTimestamp(5,obj.getLastModified());
    }

    public OperationProductCategory readValues(DataRead dr,OperationProductCategory obj) throws BasicException {
    	if(obj==null)
    		obj=new OperationProductCategory();
    	obj.setID(dr.getString(1));
    	obj.setName(dr.getString(2));
    	obj.setM_sParentID(dr.getString(3));
    	obj.setHot(dr.getBoolean(4));
        obj.setLastModified(dr.getTimestamp(5));
         return obj;
    }

    @Override
    public Class getSuportClass() {
        return OperationProductCategory.class;
    }
    
    public List<OperationProductCategory> listOperationProductCategoryByProduct(String productId)throws BasicException {
    	return new PreparedSentence(s,getTableDefinition().getListSQL("PC")+ ",OPERATIONPRODUCT P WHERE P.OPERATIONCATEGORIE=PC.ID AND P.PRODUCT=?  ", SerializerWriteString.INSTANCE, this).list(productId);
    }
}
